package com.lb.shopguide.ui.fragment.boss.market;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.AdapterActivityApply;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.market.ActivityApplyBean;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;

import butterknife.BindView;

public class FragmentHistoryApply extends BaseCommonFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private AdapterActivityApply mAdapterActivityApply;

    private ArrayList<ActivityApplyBean> mActivityApplyBeanList;

    public static FragmentHistoryApply newInstance() {
        return new FragmentHistoryApply();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_history_apply;
    }

    @Override
    public void initData() {
        mActivityApplyBeanList = new ArrayList<>();
        sendRequestGetActivityApplys();
    }

    @Override
    public void initView() {
        noDataView = LayoutInflater.from(mContext).inflate(R.layout.layout_empty_view_boss, (ViewGroup) mRecyclerView.getParent(), false);
        TextView tvEmpty = noDataView.findViewById(R.id.tv_empty);
        tvEmpty.setText("暂无申请历史");

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(253, 91, 89));
        ntb.setTitleText("申请历史");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterActivityApply = new AdapterActivityApply(R.layout.item_activity_apply, mActivityApplyBeanList, true);
        mRecyclerView.setAdapter(mAdapterActivityApply);
    }

    @Override
    public void finishRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentHistoryApply.class.getName();
    }

    @Override
    public void onRefresh() {
        mAdapterActivityApply.setEnableLoadMore(false);
        if (mError) {
            mAdapterActivityApply.setEmptyView(errorView);
            mError = false;
        } else {
            if (mNoData) {
                mAdapterActivityApply.setEmptyView(noDataView);
                mNoData = false;
            } else {
                refresh();
            }
        }
    }

    public void refresh() {
        mActivityApplyBeanList = new ArrayList<>();
        sendRequestGetActivityApplys();
    }

    private void sendRequestGetActivityApplys() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    if (response.getReturnContent() != null) {
                        JsonArray jsonArrayHistoryApplys = JsonUtil.getJsonArrayFromMap(response.getReturnContent());
                        if (jsonArrayHistoryApplys.size() != 0) {
                            for (JsonElement obj : jsonArrayHistoryApplys) {
                                ActivityApplyBean bean = JsonUtil.getGson().fromJson(obj, ActivityApplyBean.class);
                                mActivityApplyBeanList.add(bean);
                            }
                            mAdapterActivityApply.setNewData(mActivityApplyBeanList);
                        } else {
                            bindEmpty();
                        }
                        finishRefresh();
                    }
                }
            }
        };
        ApiMethodBoss.listActivityApplication(new SimpleLbObserver(listener, otherListener), AppConstant.HISTORY_APPLYS, this, FragmentEvent.DESTROY_VIEW);
    }

    private void bindEmpty() {
        mActivityApplyBeanList.clear();
        mAdapterActivityApply.setNewData(mActivityApplyBeanList);

        mNoData = true;
        onRefresh();
    }
}
